﻿using MJTop.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Dos.Captcha_使用.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            //初始化Session，否则客户端不会生成ASP.NET_SessionId的Cookie，也将导致session直接丢失。
            HttpContext.Session["test"] = "123456";
            return View();
        }

        //Dos.Captcha: http://www.itdos.com/CSharp/201605/31235542634.html
        public void CaptchaImage()
        {
            #region 一些测试设置
            var config = new CaptchaControl();
            //背景是否动态
            config.IsBackgroundDynamic = true;
            //验证码是否动态
            config.IsForegroundDynamic = true;
            config.Height = 50;
            config.Width = 150;
            //设置字体
            config.Fonts = new List<CaptchaFontArrayItem>()
            {
                new CaptchaFontArrayItem(new Font("Broadway",1)),
                new CaptchaFontArrayItem(new Font("Cooper Black",1))
            };
            //背景颜色
            config.BackColor = Color.Bisque;

            config.ExpireTime = 20 * 60;

            #endregion

            var h = new Captcha();
            h.ProcessRequest();
        }
        public JsonResult Check(string Captcha)
        {
            try
            {
                var cc = new CaptchaControl();
             
                //HttpContext.Session[cc.SessionName] == null
                if (cc.CaptchaIsExpires())
                {
                    return Json("验证码session已失效！");
                }
                //HttpContext.Session[cc.SessionName].ToString().ToUpper() == Captcha.ToUpper()
                if (cc.CaptchaValidate(Captcha))
                {
                    return Json("验证码输入正确！");
                }
                return Json("验证码输入错误！正确值：" + HttpContext.Session[new CaptchaControl().SessionName] + "。您输入的值：" + Captcha);
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }
    }
}